<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            // 更改visibility字段为更详细的权限控制
            if (Schema::hasColumn('posts', 'visibility')) {
                // 先删除旧的visibility字段
                $table->dropColumn('visibility');
            }

            // 添加新的权限控制字段
            $table->enum('access_level', ['public', 'login_required', 'followers_only', 'private'])
                  ->default('public')
                  ->after('status')
                  ->comment('访问权限：public-公开, login_required-需登录, followers_only-仅关注者, private-私密');

            // 添加是否允许评论
            if (!Schema::hasColumn('posts', 'allow_comments')) {
                $table->boolean('allow_comments')->default(true)->after('access_level');
            }

            // 添加审核相关字段
            if (!Schema::hasColumn('posts', 'is_approved')) {
                $table->boolean('is_approved')->default(false)->after('allow_comments');
            }
            if (!Schema::hasColumn('posts', 'approved_at')) {
                $table->timestamp('approved_at')->nullable()->after('is_approved');
            }
            if (!Schema::hasColumn('posts', 'approved_by')) {
                $table->unsignedBigInteger('approved_by')->nullable()->after('approved_at');
            }
            if (!Schema::hasColumn('posts', 'rejected_reason')) {
                $table->text('rejected_reason')->nullable()->after('approved_by');
            }

            // 添加阅读进度跟踪
            if (!Schema::hasColumn('posts', 'reading_time')) {
                $table->unsignedInteger('reading_time')->default(0)->after('rejected_reason')
                      ->comment('预计阅读时间（分钟）');
            }

            // 添加索引
            $table->index(['access_level']);
            $table->index(['is_approved']);
            $table->index(['user_id', 'access_level']);

            // 添加外键约束
            if (!Schema::hasColumn('posts', 'approved_by')) {
                $table->foreign('approved_by')->references('id')->on('users')->nullOnDelete();
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            // 删除外键约束
            if (Schema::hasColumn('posts', 'approved_by')) {
                $table->dropForeign(['approved_by']);
            }

            // 删除索引
            $table->dropIndex(['access_level']);
            $table->dropIndex(['is_approved']);
            $table->dropIndex(['user_id', 'access_level']);

            // 删除新增字段
            $columns = [
                'access_level', 'allow_comments', 'is_approved',
                'approved_at', 'approved_by', 'rejected_reason', 'reading_time'
            ];

            foreach ($columns as $column) {
                if (Schema::hasColumn('posts', $column)) {
                    $table->dropColumn($column);
                }
            }

            // 恢复原visibility字段
            $table->enum('visibility', ['public', 'private', 'draft'])
                  ->default('public')
                  ->after('status');
        });
    }
};